#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project : chat_model 
# @File    : chat_model_logic.py
# @IDE     : PyCharm 
# @Author  :ZH
# @Time    : 2025/1/3 10:04
__all__ = ['chat_model_logic']

from tortoise.expressions import Q

from apps import cruds
from apps.api.v1.chat_api.schema import ChatModelListQuery


class ChatModelLogic:
    def __init__(self):
        self.crud = cruds.chat_model_crud

    async def chat_model_list(self, query: ChatModelListQuery):
        skip = (query.page - 1) * query.limit
        query_sql = self.crud.model.filter(is_delete=False)
        if query.name:
            query_sql = query_sql.filter(Q(model_name__icontains=query.name) | Q(firm__icontains=query.name))
        count = await query_sql.count()
        if count:
            data = await query_sql.order_by('-id').offset(skip).limit(query.limit).all()
        else:
            data = []
        return count, data

    async def get_default_model(self):
        return await self.crud.get_one(is_default=True, is_delete=False)

    async def get_model_object(self, model_id: int):
        return await self.crud.model.filter(is_delete=False, id=model_id).first()


chat_model_logic = ChatModelLogic()
